package com.yili.oa.service.impl;

import com.yili.oa.dao.UserExchangeMapper;
import com.yili.oa.dao.UserRechargeMapper;
import com.yili.oa.models.UserExchange;
import com.yili.oa.models.UserExchangeExample;
import com.yili.oa.models.UserRecharge;
import com.yili.oa.models.UserRechargeExample;
import com.yili.oa.service.TurnoverService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.request.UserExchangeReq;
import com.yili.oa.vo.request.UserExchangeSearch;
import com.yili.oa.vo.request.UserRechargeReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/25 0025
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Service
@DataBase(value = "lyt_game_db")
public class TurnoverSvImpl implements TurnoverService {

    @Autowired
    private UserExchangeMapper mapper;

    @Autowired
    private UserRechargeMapper userRechargeMapper;

    @Override
    public List<UserExchange> getList(UserExchangeSearch searchBase, String userId) {
        UserExchangeExample example = new UserExchangeExample();
        UserExchangeExample.Criteria criteria = example.createCriteria();
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
            example.setOrderByClause("time DESC");
            return mapper.selectByExample(example);
        }
        if (StringUtils.isNotBlank(searchBase.getId())){
            criteria.andUserIdEqualTo(searchBase.getId());
        }
        if (searchBase.getStatus()!=null){
            criteria.andStatusEqualTo(searchBase.getStatus());
        }
        if (StringUtils.isNotBlank(searchBase.getBeginStr())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Long time = null;
            try {
                Date date = sdf.parse(searchBase.getBeginStr());
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andTimeGreaterThanOrEqualTo(time);
        }
        if (StringUtils.isNotBlank(searchBase.getEndStr())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Long time = null;
            try {
                Date date = sdf.parse(searchBase.getEndStr());
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andTimeLessThanOrEqualTo(time);
        }
        example.setOrderByClause("status ASC,time DESC");
        return mapper.selectByExample(example);
    }

    @Override
    public void update(UserExchangeSearch userExchangeReq) {
        Integer id = Integer.valueOf(userExchangeReq.getId());
        UserExchange userExchange = mapper.selectByPrimaryKey(id);
        if (userExchange==null){
            throw new AjaxException("找不到此记录");
        }
        userExchange.setStatus(userExchangeReq.getStatus());
        mapper.updateByPrimaryKeySelective(userExchange);
    }

    @Override
    public UserExchange addExchange(UserExchangeReq userExchangeReq) {
        UserExchange userExchange = new UserExchange();
        BeanUtils.copyProperties(userExchangeReq,userExchange);
        //获取当前时间
        Date date = new Date();
        //设置要获取的时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //获取String类型的时间
        String createdate = sdf.format(date);
        userExchange.setOrderId("EG-"+createdate);
        try {
            mapper.insert(userExchange);
        }catch (Exception e){
            return null;
        }
        return userExchange;
    }

    @Override
    public UserRecharge addRecharge(UserRechargeReq userRechargeReq) {
        UserRecharge userRecharge = new UserRecharge();
        BeanUtils.copyProperties(userRechargeReq,userRecharge);
        //获取当前时间
        Date date = new Date();
        //设置要获取的时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //获取String类型的时间
        String createdate = sdf.format(date);
        userRecharge.setOrderId("RG-"+createdate);
        try {
            userRechargeMapper.insert(userRecharge);
        }catch (Exception e){
            return null;
        }
        return userRecharge;
    }

    @Override
    public List<UserRecharge> getRechargeList(UserExchangeSearch searchBase, String userId) {
        UserRechargeExample example = new UserRechargeExample();
        UserRechargeExample.Criteria criteria = example.createCriteria();
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
            example.setOrderByClause("time DESC");
            return userRechargeMapper.selectByExample(example);
        }
        if (StringUtils.isNotBlank(searchBase.getId())){
            criteria.andUserIdEqualTo(searchBase.getId());
        }
        if (searchBase.getStatus()!=null){
            criteria.andStatusEqualTo(searchBase.getStatus());
        }
        if (StringUtils.isNotBlank(searchBase.getBeginStr())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Long time = null;
            try {
                Date date = sdf.parse(searchBase.getBeginStr());
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andTimeGreaterThanOrEqualTo(time);
        }
        if (StringUtils.isNotBlank(searchBase.getEndStr())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Long time = null;
            try {
                Date date = sdf.parse(searchBase.getEndStr());
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andTimeLessThanOrEqualTo(time);
        }
        example.setOrderByClause("status ASC,time DESC");
        return userRechargeMapper.selectByExample(example);
    }
}
